// pages/medical-record-delivery/application-form.js
Page({
	data: {
		formData: {
			dischargeStatus: 'discharged', // 默认已出院
			deliveryMethod: 'mail', // 默认邮寄
			recipientName: '',
			recipientPhone: '',
			selectedCity: '',
			recipientAddress: ''
		},
		showCityPicker: false,
		cityList: [
			{ id: 1, name: '北京市' },
			{ id: 2, name: '上海市' },
			{ id: 3, name: '广州市' },
			{ id: 4, name: '深圳市' },
			{ id: 5, name: '杭州市' },
			{ id: 6, name: '南京市' },
			{ id: 7, name: '武汉市' },
			{ id: 8, name: '成都市' },
			{ id: 9, name: '西安市' },
			{ id: 10, name: '天津市' },
			{ id: 11, name: '重庆市' },
			{ id: 12, name: '青岛市' },
			{ id: 13, name: '大连市' },
			{ id: 14, name: '厦门市' },
			{ id: 15, name: '苏州市' },
			{ id: 16, name: '无锡市' },
			{ id: 17, name: '宁波市' },
			{ id: 18, name: '佛山市' },
			{ id: 19, name: '东莞市' },
			{ id: 20, name: '中山市' }
		]
	},

	onLoad(options) {
		console.log('病案寄送申请表单页面加载')
		this.loadCityList()
	},

	// 加载城市列表
	loadCityList() {
		wx.request({
			url: 'http://localhost:8080/cities',
			method: 'GET',
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				if (res.statusCode === 200 && res.data) {
					let cityData = res.data
					
					// 处理数据格式
					if (typeof cityData === 'string') {
						try {
							cityData = JSON.parse(cityData)
						} catch (e) {
							console.error('JSON解析失败:', e)
							return
						}
					}
					
					if (!Array.isArray(cityData)) {
						if (cityData.data && Array.isArray(cityData.data)) {
							cityData = cityData.data
						} else if (cityData.list && Array.isArray(cityData.list)) {
							cityData = cityData.list
						} else {
							console.error('城市数据格式不正确')
							return
						}
					}
					
					// 处理城市数据
					const processedData = cityData.map(item => ({
						id: item.id || item.cityId,
						name: item.name || item.cityName
					}))
					
					this.setData({
						cityList: processedData
					})
				}
			},
			fail: (error) => {
				console.error('加载城市列表失败:', error)
			}
		})
	},

	// 选择出院状态
	selectDischargeStatus(e) {
		const status = e.currentTarget.dataset.status
		this.setData({
			'formData.dischargeStatus': status
		})
		console.log('选择出院状态:', status)
	},

	// 选择领取方式
	selectDeliveryMethod(e) {
		const method = e.currentTarget.dataset.method
		this.setData({
			'formData.deliveryMethod': method
		})
		console.log('选择领取方式:', method)
	},

	// 收件人姓名输入
	onRecipientNameInput(e) {
		this.setData({
			'formData.recipientName': e.detail.value
		})
	},

	// 收件人手机号输入
	onRecipientPhoneInput(e) {
		this.setData({
			'formData.recipientPhone': e.detail.value
		})
	},

	// 收件人地址输入
	onRecipientAddressInput(e) {
		this.setData({
			'formData.recipientAddress': e.detail.value
		})
	},

	// 显示城市选择器
	showCityPicker() {
		this.setData({
			showCityPicker: true
		})
	},

	// 关闭城市选择器
	closeCityPicker() {
		this.setData({
			showCityPicker: false
		})
	},

	// 选择城市
	selectCity(e) {
		const city = e.currentTarget.dataset.city
		this.setData({
			'formData.selectedCity': city.name,
			showCityPicker: false
		})
		console.log('选择城市:', city.name)
	},

	// 阻止事件冒泡
	stopPropagation() {
		// 阻止事件冒泡
	},

	// 下一步
	nextStep() {
		// 表单验证
		if (this.data.formData.deliveryMethod === 'mail') {
			if (!this.data.formData.recipientName.trim()) {
				wx.showToast({
					title: '请输入收件人姓名',
					icon: 'none'
				})
				return
			}
			if (!this.data.formData.recipientPhone.trim()) {
				wx.showToast({
					title: '请输入收件人手机号',
					icon: 'none'
				})
				return
			}
			if (!this.data.formData.selectedCity) {
				wx.showToast({
					title: '请选择城市',
					icon: 'none'
				})
				return
			}
			if (!this.data.formData.recipientAddress.trim()) {
				wx.showToast({
					title: '请输入详细地址',
					icon: 'none'
				})
				return
			}
		}

		// 保存表单数据到本地存储
		wx.setStorageSync('medicalRecordFormData', this.data.formData)

		// 跳转到证件上传页面
		wx.navigateTo({
			url: '/pages/medical-record-delivery/document-upload'
		})
	}
}) 